package JVM;

import java.util.HashMap;
import java.util.Map;
import java.util.WeakHashMap;

public class WeakHashMapDemo {

    private void hashMapDemo(){
        Map<Integer, String> map = new HashMap<>();
        Integer key = new Integer(1);
        String value = "HashMap";
        map.put(key, value);
        System.out.println(map);

        key = null;
        System.out.println(map);
    }

    private void weakHashMapDemo(){
        WeakHashMap<Integer, String> map = new WeakHashMap<>();
        Integer key = new Integer(1);
        String value = "WeakHashMap";
        map.put(key, value);
        System.out.println(map);

        key = null;
        System.out.println(map);

        System.gc();
        System.out.println(map);
    }

    public static void main(String[] args) {
        WeakHashMapDemo demo = new WeakHashMapDemo();
        demo.hashMapDemo();
        System.out.println("===================");
        demo.weakHashMapDemo();
    }
}
